#include "ShotAheadStrategy.h"
#include "WeaponBank.h"


ShotAheadStrategy::ShotAheadStrategy(void)
{
	_weapon = NULL;
	_numshoot = 0;
	_timeDelay = 0;
}


ShotAheadStrategy::~ShotAheadStrategy(void)
{
	CC_SAFE_RELEASE(_weapon);
}

void ShotAheadStrategy::processing( float dt )
{
}

void ShotAheadStrategy::parseXml( xml_node node )
{
	char* weaponKey = (char*)node.child("Weapon").text().as_string();
	setNumShoot(node.child("NumShoot").text().as_int());
	setTimeDelay(node.child("TimeDelay").text().as_float());
	Weapon* wp = WeaponBank::sharedWeaponBank()->getWeapon(weaponKey);
	setWeapon(wp);
}

bool ShotAheadStrategy::init()
{
	if (!BaseStrategy::init())
		return false;

	return true;
}

void ShotAheadStrategy::onEnter()
{
	BaseStrategy::onEnter();
	schedule(SEL_SCHEDULE(&ShotAheadStrategy::shoot), _timeDelay, _numshoot - 1, 0);
}

void ShotAheadStrategy::onExit()
{
	BaseStrategy::onEnter();
	unschedule(SEL_SCHEDULE(&ShotAheadStrategy::shoot));
}

void ShotAheadStrategy::shoot(float dt)
{
	if (_weapon)
		_weapon->addBullet();
}

Weapon* ShotAheadStrategy::getWeapon()
{
	return _weapon;
}

void ShotAheadStrategy::setWeapon(Weapon* var)
{
	if (_weapon != var)
	{
		removeChild(_weapon);
		CC_SAFE_RETAIN(var);
		CC_SAFE_RELEASE(_weapon);
		_weapon = var;
		addChild(_weapon);
	}
}